<template>
	<view class="wrap">
		<view class="nav">
			<g-nav :navlist="navList" :navindex="navIndex" @click="onNavClick"></g-nav>
		</view>
		<view class="gap"></view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="onSkipTap('goodsDetail', item.id)">
				<view class="item-img">
					<image :src="item.cover" mode=""></image>
				</view>
				<view class="item-main">
					<view class="item-title">{{item.productName}}</view>
					<view class="item-mainB">
						<view class="item-mainB_L">
							<text>￥{{item.price}}/{{item.unit}}</text>
							<text>（{{item.emptyDiff}}）</text>
						</view>
						<view class="item-mainB_M">货号：{{item.id}}</view>
						<view class="item-mainB_R">
							<view class="item-btn upShelf" v-if="item.isUp == 1"
								@click.stop="onSaleOffSale(item.id, 0)">上架
							</view>
							<view class="item-btn" v-else @click.stop="onSaleOffSale(item.id, 1)">下架</view>
						</view>
					</view>
				</view>
			</view>
			<no-more v-if="page == totalPage && hasLoad"></no-more>
			<no-data v-if="hasLoad && list.length == 0"></no-data>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				navList: [{
					name: '全部',
					type: '',
				}, {
					name: '在售',
					type: 0,
				}, {
					name: '已下架',
					type: 1,
				}],
				navIndex: 0,
				list: [],
				page: 1,
				totalPage: 1,
				size: 15,
				hasLoad: false,
				noMore: false,
			}
		},
		onLoad(options) {
			this.id = options.id
			this.updatePage()
		},
		onPullDownRefresh() {
			this.updatePage()
		},
		onReachBottom() {
			this.page++
			this.getList()
		},
		methods: {
			updatePage() {
				this.resetPage()
				this.getList()
			},
			resetPage() {
				this.list = []
				this.page = 1
				this.totalPage = 1
				this.size = 15
				this.hasLoad = false
				this.noMore = false
			},
			onNavClick(index) {
				this.navIndex = index
				this.updatePage()
			},
			onSkipTap(type, params) {
				let urlObj = {
					goodsDetail: () => {
						uni.navigateTo({
							url: `/pages/shopInfo/goodsDetail?id=${params}`
						})
					}
				}
				this.$g_util.navigationTo(urlObj, type)
			},
			getList() {
				let params = {
					pageNum: this.page,
					pageSize: this.size,
					productTypeId: this.id,
					createUserId: this.gUserInfo.userId
				}
				if (this.navList[this.navIndex].type != -1) {
					params.isUp = this.navList[this.navIndex].type
				}
				uni.showLoading()
				this.$g_api.productSearchProducts(params).then(({
					rows,
					totalPage
				}) => {
					uni.stopPullDownRefresh()
					this.hasLoad = true
					this.totalPage = totalPage
					if (rows?.length > 0) {
						rows.forEach(item => {
							// item.createTimeStr = new Date(item.createTime).Format('yyyy-MM-dd')
							// item.secretTel = item.tel.slice(0, 3) + '****' + item.tel.slice(7, 11)
							item.cover = this.$g_util.concatRequestUrl(item.cover)
						})
						this.list = this.list.concat(rows)
					} else {
						this.noMore = true
					}
				}).finally(() => {
					uni.hideLoading()
				})
			},
			/**
			 * 下架
			 */
			onSaleOffSale(id, isUp) {
				this.$g_api.productProductUpDown({
					id,
					isUp
				}).then(({
					data
				}) => {
					if (isUp == 1) {
						uni.showToast({
							title: '下架成功'
						})
					} else if (isUp === '0' || isUp === 0) {
						uni.showToast({
							title: '上架成功'
						})
					}
					this.updatePage()
				})
			}
		},
	}
</script>
<style scoped lang="scss">
	.wrap {
		padding-top: 90rpx;
	}

	.nav {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		z-index: 999;
	}

	.gap {
		width: 100vw;
		height: 10rpx;
		background-color: #fff;
	}

	.list {
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.item {
		display: flex;
		padding: 30rpx;
		border-top: 1px solid $g-border-color;
		background-color: #fff;
	}

	.item-img {
		width: 140rpx;
		font-size: 0;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.item-main {
		flex: 1;
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.item-mainB {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.item-mainB_L {
		text {
			&:first-child {
				color: #FF4C4C;
				font-size: 26rpx;
			}

			&:last-child {
				color: #999999;
				font-size: 20rpx;
			}
		}
	}

	.item-mainB_M {
		font-size: 24rpx;
		color: #999;
	}

	.item-mainB_R {
		width: 140rpx;
		display: flex;
		justify-content: flex-end;
	}

	.item-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 120rpx;
		height: 60rpx;
		// line-height: 60rpx;
		// text-align: center;
		border-radius: 10rpx;
		font-size: 24rpx;
		color: #333;
		border: 1px solid #ccc;
		box-sizing: border-box;

		&.upShelf {
			color: $g-main-color;
			border: 1px solid $g-main-color;
		}
	}
</style>
